package 并发编程13.LockSupport工具类;

import java.util.concurrent.locks.LockSupport;

/**
 * locksupport工具
 * @author idea
 * @data 2019/2/1
 */
public class LockSupportDemo {

    public static void main(String[] args) {
        TestThread testThread=new TestThread(Thread.currentThread());
        testThread.start();
        System.out.println("before park");
        //堵塞一个叫做Park的线程对象
        LockSupport.park("Park");
        System.out.println("after park!222");
    }
}

class TestThread extends Thread{
    private Object object;

    public TestThread(Object object){
        this.object=object;
    }

    @Override
    public void run(){
        System.out.println("before unpark");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 获取blocker
        System.out.println("blocker info"+LockSupport.getBlocker( (Thread) object ));
        //唤醒当前处于阻塞的线程
        LockSupport.unpark( (Thread) object );

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("blocker info:"+LockSupport.getBlocker((Thread) object ));
        System.out.println("after unpark!");

    }
}
